package io;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;

public class App {

	public static void main(String[] args) throws Exception{
		byte[] b1 = "您".getBytes();
		byte[] b2 = "好".getBytes();
		printArray(b1);
		printArray(b2);
		
		ByteArrayInputStream bai1 = new ByteArrayInputStream(b1);
		ByteArrayInputStream bai2 = new ByteArrayInputStream(b2);
		SequenceInputStream sis = new SequenceInputStream(bai1,bai2);
		
		int data;
		while((data=sis.read())!=-1) {
			System.out.print(data+" ");
		}
		
		sis.close();
		
		System.out.println();
		System.out.println((int)'您');
		System.out.println((int)'好');
		System.out.println((int)'老');
		System.out.println((short)'老');
		
		System.out.println();
		//DataInputStream dis = new DataInputStream(new ByteArrayInputStream("测试啊".getBytes()));
		printTextFileByByte("text-GBK.txt");
		printTextFileByByte("text-UTF.txt");
		
		System.out.println();
		
		printArray("12测试用".getBytes());
		printArray("12测试用".getBytes("UTF-8"));
		
		printTextFileByChar("text-GBK.txt", "GBK");
		printTextFileByChar("text-UTF.txt", "UTF-8");

	}
	
	public static void printArray(byte[] a) {
		for(byte b : a) {
			System.out.print(b + " ");
		}
		System.out.println();
	}
	
	public static void printTextFileByByte(String relativePath) throws Exception{
		InputStream dis = App.class.getResourceAsStream(relativePath);
		int i;
		while((i=dis.read()) != -1)
		{
			System.out.print(i + " ");
		}
		System.out.println();
	}
	
	private static void printTextFileByChar(String relativePath, String encoding) throws Exception{
		InputStream dis = App.class.getResourceAsStream(relativePath);
		InputStreamReader isr = new InputStreamReader(dis, encoding);
		int i;
		while((i=isr.read()) != -1)
		{
			System.out.print((char)i + " ");
		}
		System.out.println();
	}

}

class App2 {
	public static void main(String[] args) throws Exception{
		FileOutputStream fos = new FileOutputStream("test.txt");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		DataOutputStream dos = new DataOutputStream(bos);
		
		dos.writeUTF("我是UTF啊");
		dos.write("试试啊".getBytes("UTF-8"));
		//dos.writeChar('哇');
		dos.close();
	}
}

class App3 {
	public static void main(String[] args) throws Exception{
		RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
		raf.seek(2L);
		raf.writeChar('林');
	}
}